#ifndef __KEY_MODULE_H__
#define __KEY_MODULE_H__
#include "stdint.h"
#include "gd32f4xx.h"
#include "bs_app_conf.h"
#include "bs_app.h"


#define KEY_NUM                           3     // 按键个数
#define KEY_DEBOUNCE_TIME                 30    // 按键消抖时间，单位：ms
#define LONG_PRESS_TIME                   1000  // 长按时间，单位：ms
#define DOUBLE_CLICK_TIME                 350   // 双击间隔时间，在此时间内按第二下，才算双击，单位：ms


typedef enum {
    KEY_ID_0 = 0,
    KEY_ID_1,
    KEY_ID_2,
} bs_key_id_t;


typedef struct bs_key_pinmap {
    bs_key_id_t     id;
    uint32_t        port;
    uint32_t        pin;
} bs_key_pinmap_t;


typedef enum{
    KEY_IDLE = 0,
    KEY_PRESSED,
    KEY_RELEASED,
    KEY_SINGLE_CLICK,
    KEY_DOUBLE_CLICK,
    KEY_LONG_PRESS,
}bs_key_state_t;



typedef struct bs_key
{
    bs_key_id_t      id;
    bs_key_state_t   state;
    int32_t          last_press_time;
    int32_t          last_release_time;
    int32_t          debounce_time;
    uint8_t          stable_status : 1;
    uint8_t          last_status   : 1;
    uint8_t          awaiting_dblclick : 1;
} bs_key_t;


typedef void  (*key_event_handler_t)(bs_key_t *key);


typedef struct key_manager{
    bs_key_t keys[KEY_NUM];
    key_event_handler_t on_single_click;
    key_event_handler_t on_double_click;
    key_event_handler_t on_long_press;
    key_event_handler_t on_press;     
    key_event_handler_t on_release;    

    int32_t  timer;
    int32_t  long_press_time;
    int32_t  double_click_time;
}key_manager_t;


void key_module_init(void);
void bs_key_process(void);
#endif


